<?php
namespace application\datatypes;
use application\datatypes\DataType;

class GoogleMapsLocation extends DataType {
	public static $name = 'Google Maps Location';
	public static $fields = array(array('column'=>'latitude', 'columnType'=>'FLOAT(10,6)'), array('column'=>'longitude', 'columnType'=>'FLOAT(10,6)'), array('column'=>'address', 'columnType'=>'VARCHAR(100)'));
	
	public static $displayParams = array(
		array(
			'label'=>'Show',
			'name'=>'show',
			'type'=>'select',
			'defaultValue'=>'address',
			'options'=>array(
				array(
					'label'=>'Address',
					'value'=>'address'
				),
				array(
					'label'=>'Map',
					'value'=>'map'
				)
			)
		)
	);
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && (!$data[$field->cf_column.'_latitude'] || !$data[$field->cf_column.'_longitude'] || !$data[$field->cf_column.'_address'])) {
			$results['errors'][] = $field->cf_display_name.' is required.';
		}
		elseif($data[$field->cf_column.'_address'] && (!$data[$field->cf_column.'_latitude'] || !$data[$field->cf_column.'_longitude'])) {
			$results['errors'][] = 'Google geocoding of address failed. Please try again.';
		}
		else {
			$results['db_fields'][$field->cf_column.'_address'] = $data[$field->cf_column.'_address'];
			$results['db_fields'][$field->cf_column.'_latitude'] = $data[$field->cf_column.'_latitude'];
			$results['db_fields'][$field->cf_column.'_longitude'] = $data[$field->cf_column.'_longitude'];
		}
		
		return $results;
	}
	
	public static function formHeader() {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript('google maps');
		Modulator()->includeScript(CN_URL."includes/third-party/typing/typing.js");
		?>
		<script type="text/javascript" src="<?=CN_URL?>modules/gmaps/gmap3.js"></script>
		<script type="text/javascript" src="<?=CN_URL?>modules/gmaps/form.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$('.google-maps-location-map').gmapForm();
			});
		</script>
		<style type="text/css"> 
			div.google-maps-location-map {
				width:300px;
				height:300px;
			}
		</style> 
		
		<?php
	}
	
	public static function displayHeader($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript('google maps');
		?>
		<script type="text/javascript" src="<?=CN_URL?>modules/gmaps/gmap3.js"></script>
		<script type="text/javascript" src="<?=CN_URL?>modules/gmaps/form.js"></script>
		<script type="text/javascript">
			function loadMap() {
				var latitude = $('#<?=$field->cf_column?>_map').attr('data-latitude');
				var longitude = $('#<?=$field->cf_column?>_map').attr('data-longitude');
				$('#<?=$field->cf_column?>_map').gmaps({
					center: {
						latitude:latitude,
						longitude:longitude
					},
					markers:[{
						latitude:latitude,
						longitude:longitude
					}],
					zoom:13
				});
			};
			$(document).ready(function() {
				loadMap();
			});
		</script>
		<style type="text/css"> 
			div.google-maps-location-map {
				width:300px;
				height:300px;
			}
		</style> 
		<?php
	}
	
	public static function display($formElements) {
		?>
			
			<fieldset class="gmaps-location-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<div class="google-maps-location-map"></div>
			<label>Address</label>
			<?= $formElements['elements']['address']?>
			<?= $formElements['elements']['latitude']?>
			<?= $formElements['elements']['longitude']?>
			</section>
			</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$latitude = $data[$field->cf_column.'_latitude'];
		$longitude = $data[$field->cf_column.'_longitude'];
		$address = $data[$field->cf_column.'_address'];
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
						'latitude'=>
							sprintf('<input type="hidden" value="%1$s" class="short-text google-maps-location-latitude" name="%2$s" id="%2$s"/>', $latitude, $field->cf_column.'_latitude'),
						'longitude'=>
							sprintf('<input type="hidden" value="%1$s" class="short-text google-maps-location-longitude" name="%2$s" id="%2$s"/>', $longitude, $field->cf_column.'_longitude'),
						'address'=>
							sprintf('<input type="text" value="%1$s" class="short-text google-maps-location-address" name="%2$s" id="%2$s"/>', $address, $field->cf_column.'_address')
						
		));
		return $formElements;
	
	
	}
	
	public static function editForm($field) {
		?>
		<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
		<ul class="form-fields">
			<li>
				<label>Field Display Name</label>
				<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</li>
			<li>
				<label>Field Description</label>
				<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</li>
			<li>
				<label><input type="checkbox" name="field_required" <?=($field->getRequired()) ? 'checked="checked"' : ''?> value="1"/> Required</label>
				<label><input type="checkbox" name="field_show" value="1" <?=($field->getShow()) ? 'checked="checked"' : ''?>/> Show in Manage Panel</label>
				
			</li>
			
		</ul>
		<?php
	}
	
	public static function fromDB($fields, $field, $data) {
		$column = $field->cf_column;
		return array($field->cf_column.'_address' => $fields[$column.'_address'], $field->cf_column.'_latitude' => $fields[$column.'_latitude'], $field->cf_column.'_longitude' => $fields[$column.'_longitude']);
	}
	
	public static function prepareData($field, $data, $params) {
		$show = $params['show'];
		if(!$show) $show = 'address';
		
		switch($show) {
			case 'address':
				$return = $data[$field->cf_column.'_address'];
				break;
			case 'map':
				$return = '<div data-longitude="'.$data[$field->cf_column.'_longitude'].'" data-latitude="'.$data[$field->cf_column.'_latitude'].'" class="google-map" id="'.$field->cf_column.'_map"></div>';
				break;
				
		}
		
		return $return;
	}
	
}
?>